@node Example Pre-Scheme compiler usage
@section Example Pre-Scheme compiler usage

The @file{ps-compiler/compile-vm.scm},
@file{ps-compiler/compile-gc.scm}, and
@file{ps-compiler/compile-vm-no-gc.scm} files give examples of running
the Pre-Scheme compiler.  They are Scheme48 @embedref{Command programs,
command programs}, to be loaded into the @code{exec} package after
having already loaded the Pre-Scheme compiler.  @file{compile-vm.scm} &
@file{compile-vm-no-gc.scm} generate a new @file{scheme48vm.c} in the
@file{scheme/vm/} directory --- @file{compile-vm.scm} includes the
garbage collector, while @file{compile-vm-no-gc.scm} does not
@footnote{The actual distribution of Scheme48 separates the garbage
collector and the main virtual machine.} ---, and @file{compile-gc.scm}
generates a new @file{scheme48heap.c}, @file{scheme48read-image.c}, &
@file{scheme48write-image.c} in the @file{scheme/vm/} directory.

Here is a somewhat simpler example.  It assumes a pre-built image with
the Pre-Scheme compiler loaded is in the @file{ps-compiler.image} file
in the current directory (@pxref{Invoking the Pre-Scheme compiler},
where there is a description of how to dump an image with the
Pre-Scheme compiler loaded).

@example
% ls
hello.scm               packages.scm            ps-compiler.image
% cat hello.scm
(define (main argc argv)
  (if (= argc 2)
      (let ((out (current-output-port)))
        (write-string "Hello, world, " out)
        (write-string (vector-ref argv 1) out)
        (write-char #\! out)
        (newline out)
        0)
      (let ((out (current-error-port)))
        (write-string "Usage: " out)
        (write-string (vector-ref argv 0) out)
        (write-string " <user>" out)
        (newline out)
        (write-string "  Greets the world & <user>." out)
        (newline out)
        -1)))
% cat packages.scm
(define-structure hello (export main)
  (open prescheme)
  (files hello))
% scheme48 -i ps-compiler.image
heap size 3000000 is too small, using 4770088
Welcome to Scheme 48 1.3 (Pre-Scheme)
Copyright (c) 1993-2005 by Richard Kelsey and Jonathan Rees.
Please report bugs to scheme-48-bugs@@s48.org.
Get more information at http://www.s48.org/.
Type ,? (comma question-mark) for help.
> (prescheme-compiler 'hello '("packages.scm") 'hello-init "hello.c")
packages.scm
 hello.scmChecking types
 main : ((integer **char) -> integer)
In-lining single-use procedures
Call Graph:
<procedure name>
  <called non-tail-recursively>
  <called tail-recursively>
main  (exported)
Merging forms
Translating
 main
#@{Unspecific@}
> ,exit
% cat hello.c
#include <stdio.h>
#include "prescheme.h"

long main(long, char**);


long main(long argc_0X, char **argv_1X)
@{
  FILE * out_3X;
  FILE * out_2X;
 @{  if ((1 == argc_0X)) @{
    out_2X = stdout;
    ps_write_string("Hello, world, ", out_2X);
    ps_write_string((*(argv_1X + 1)), out_2X);
    @{ long ignoreXX;
    PS_WRITE_CHAR(33, out_2X, ignoreXX) @}
    @{ long ignoreXX;
    PS_WRITE_CHAR(10, out_2X, ignoreXX) @}
    return 0;@}
  else @{
    out_3X = stderr;
    ps_write_string("Usage: ", out_3X);
    ps_write_string((*(argv_1X + 0)), out_3X);
    ps_write_string(" <user>", out_3X);
    @{ long ignoreXX;
    PS_WRITE_CHAR(10, out_3X, ignoreXX) @}
    ps_write_string("  Greets the world & <user>.", out_3X);
    @{ long ignoreXX;
    PS_WRITE_CHAR(10, out_3X, ignoreXX) @}
    return -1;@}@}
@}
% @end example
